﻿using System;
using System.Collections;
using System.Web.Security;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ue.Qcdz.Domain.Membership;

namespace Ue.Qcdz.Domain.Roles
{
    public class OdbcRoleProvider : RoleProvider
    {
        private IUserRepository userepository;
        private IRoleRepository rolerepository;
        public OdbcRoleProvider()
        {
            userepository = Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IUserRepository, User>();
            rolerepository = Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IRoleRepository, Role>();
        }

        #region Overrides of RoleProvider

        public override bool IsUserInRole(string username, string roleName)
        {
            User user = userepository.FindBy(username);
            return user.Roles.Any(role => role.Name.Equals(roleName));
        }

        public override string[] GetRolesForUser(string username)
        {
            User user = userepository.FindBy(username);
            ArrayList arrayRole = new ArrayList();
            foreach (Role role in user.Roles)
            {
                arrayRole.Add(role.Name);
            }
            return (string[])arrayRole.ToArray(typeof(string));
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {

            return rolerepository.FindBy(roleName) != null;
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            ArrayList arrUserName = new ArrayList();
            userepository = Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IUserRepository, User>();
            IList<User> userList = userepository.GetUsersInRole(roleName);
            foreach (User user in userList)
            {
                arrUserName.Add(user.UserName);
            }
            return (string[])arrUserName.ToArray(typeof(string));

        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        #endregion
    }
}
